package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import model.Beneficiario;

public class BeneficiarioJDBCCadastro implements BeneficiarioCadastroDAO {

	Connection conexao;

	public BeneficiarioJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(List<Beneficiario> beneficiarioLista, String idcliente) {
		String sqlquery = "insert into beneficiario(CDCLIENTE, NMBENEFICIARIO, GRAUPARENTESCO, DTINCLUSAO, BOLATIVO, BOLDML )"
				+ " values(?,?,?,?,?,?);";

		PreparedStatement pstmt;
		int tamanho = 0;
		int tam = beneficiarioLista.size();

		Date datenow = null;
		try {
			String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			datenow = dateFormat.parse(date);
			// beneficiarioLista.get(tamanho).getDtinclusao(datenow);
		} catch (Exception e) {
			e.printStackTrace();
		}

		while (tamanho <= (tam - 1)) {

			try {
				pstmt = this.conexao.prepareStatement(sqlquery);
				pstmt.setString(1, idcliente);
				pstmt.setString(2, beneficiarioLista.get(tamanho)
						.getNmbeneficiario());
				pstmt.setString(3, beneficiarioLista.get(tamanho)
						.getGrauparentesco());
				// pstmt.setDate(4, new
				// java.sql.Date(beneficiarioLista.get(tamanho).getDtinclusao().getTime()));
				pstmt.setDate(4, new java.sql.Date(datenow.getTime()));
				pstmt.setString(5, "A");
				pstmt.setString(6, "I");
				// pstmt.setTime(8, new
				// java.sql.Time(formapagto.getHrinclusao().getTime()));
				// pstmt.setDate(7, new
				// java.sql.Date(dependentes.getDtalteracao().getTime()));
				// pstmt.setTime(10, new
				// java.sql.Time(formapagto.getHralteracao().getTime()));
				// pstmt.setString(8, dependentes.getBolativo());
				pstmt.execute();
				// return("success");

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("VALOR EM BRANCO " + sqlquery
						+ " ++++++++++++++ ");
				System.out.println("TAMANHO " + tamanho + " ++++++++++++++ ");
				// System.out.println("TAMANHO "+val+" ++++++++++++++ ");
				return ("error");
			}
			tamanho++;
		}
		beneficiarioLista.clear();
		return "success";
	}

	/**
	 * public List<String> ListarTpServico(String nmtpservico){
	 * 
	 * List<String> result = new ArrayList<String>(); if
	 * (!nmtpservico.trim().equals("")) {
	 * 
	 * String sql =
	 * "SELECT CDTPSERVICO, NMTPSERVICO FROM TPSERVICO WHERE NMTPSERVICO like '%"
	 * + nmtpservico + "%' ORDER BY NMTPSERVICO ASC;"; String snmtpservico = "";
	 * String scdtpservico = ""; try { java.sql.Statement stmt =
	 * conexao.createStatement(); ResultSet valuetpservico =
	 * stmt.executeQuery(sql);
	 * 
	 * valuetpservico.first(); while (!valuetpservico.isAfterLast()){
	 * snmtpservico = valuetpservico.getString("NMTPSERVICO"); scdtpservico =
	 * valuetpservico.getString("CDTPSERVICO"); result.add(snmtpservico);
	 * //result.add(scdtpservico+" | "+snmtpservico); valuetpservico.next(); }
	 * 
	 * } catch (Exception e) { e.printStackTrace(); } }else {
	 * System.out.println(
	 * "VALOR EM BRANCO "+nmtpservico+" ------------- "+result ); } return
	 * result; }
	 * 
	 * public String LocalizaTpServico(String nmtpservico){
	 * 
	 * String result = ""; if (!nmtpservico.trim().equals("")) {
	 * 
	 * String sql =
	 * "SELECT CDTPSERVICO FROM TPSERVICO WHERE (NMTPSERVICO like '"
	 * +nmtpservico+"') ORDER BY CDTPSERVICO ASC;"; String scdtpservico = "";
	 * try { java.sql.Statement stmt = conexao.createStatement(); ResultSet
	 * valuetpservico = stmt.executeQuery(sql);
	 * 
	 * valuetpservico.first(); scdtpservico =
	 * valuetpservico.getString("CDTPSERVICO"); //result.add(scdregistro);
	 * result = (scdtpservico);
	 * System.out.println("TP SERVICO "+scdtpservico+" ------------- "+result );
	 * 
	 * } catch (Exception e) { e.printStackTrace(); } }else {
	 * System.out.println(
	 * "VALOR EM BRANCO "+nmtpservico+" ------------- "+result ); } return
	 * result; }
	 **/

	public ResultSet editarConsulta(String idcliente) {

		String sql = "select CDCLIENTE, NMBENEFICIARIO, GRAUPARENTESCO, DATE_FORMAT(DTINCLUSAO, '%d/%m/%Y') AS DTINCLUSAO, BOLATIVO "
				+ "from beneficiario where CDCLIENTE = " + idcliente + ";";

		ResultSet value = null;
		try {
			java.sql.Statement stmt = conexao.createStatement();
			value = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	public String atualizar(List<Beneficiario> beneficiarioLista) {
		String sqlquery = "update beneficiario set NMBENEFICIARIO = ?, GRAUPARENTESCO = ?, DTALTERACAO = ?, BOLDML = ?, BOLATIVO = ? "
				+ "where CDCLIENTE = ? ;";

		PreparedStatement pstmt;
		int tamanho = 0;
		int tam = beneficiarioLista.size();

		Date datenow = null;
		try {
			String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			datenow = dateFormat.parse(date);
			// beneficiarioLista.get(tamanho).getDtinclusao(datenow);
		} catch (Exception e) {
			e.printStackTrace();
		}

		while (tamanho <= (tam - 1)) {

			try {
				pstmt = this.conexao.prepareStatement(sqlquery);
				// pstmt.setString(1, idcliente );
				pstmt.setString(1, beneficiarioLista.get(tamanho)
						.getNmbeneficiario());
				pstmt.setString(2, beneficiarioLista.get(tamanho)
						.getGrauparentesco());
				// pstmt.setDate(4, new
				// java.sql.Date(beneficiarioLista.get(tamanho).getDtinclusao().getTime()));
				pstmt.setDate(3, new java.sql.Date(datenow.getTime()));
				pstmt.setString(4, "U");

				if (beneficiarioLista.get(tamanho).getCdcliente()
						.equals("true")) {
					pstmt.setString(5, "A");
				} else {
					pstmt.setString(5, "I");
				}
				pstmt.setString(6, beneficiarioLista.get(tamanho)
						.getCdcliente());
				// pstmt.setTime(8, new
				// java.sql.Time(formapagto.getHrinclusao().getTime()));
				// pstmt.setDate(7, new
				// java.sql.Date(dependentes.getDtalteracao().getTime()));
				// pstmt.setTime(10, new
				// java.sql.Time(formapagto.getHralteracao().getTime()));
				// pstmt.setString(8, dependentes.getBolativo());
				pstmt.execute();
				// return("success");

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("VALOR EM BRANCO " + sqlquery
						+ " ++++++++++++++ ");
				System.out.println("TAMANHO " + tamanho + " ++++++++++++++ ");
				// System.out.println("TAMANHO "+val+" ++++++++++++++ ");
				return ("error");
			}
			tamanho++;
		}
		beneficiarioLista.clear();
		return "success";
	}

	public void excluir(Beneficiario beneficiario) {
	}

	public void editar(Beneficiario beneficiario) {
	}

	public List<Beneficiario> ListarDependentesT() {
		return null;
	}

	@Override
	public List<Beneficiario> ListarBeneficiario() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Beneficiario> ListarBeneficiarioT() {
		// TODO Auto-generated method stub
		return null;
	}

}
